package com.hzxy.yiyid.web.module.controller;

import com.hzxy.yiyid.web.common.base.BaseResult;
import com.hzxy.yiyid.web.common.error.ErrorCodeEnum;
import com.hzxy.yiyid.web.module.controller.vo.NearbyOrganizationRequestVO;
import com.hzxy.yiyid.web.module.service.OrganizationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName OrganizationController
 * @Description TOOD
 * @Author RAICOM2023
 * @Date 2023/3/6 23:00
 * @Version 1.0
 **/
@Api(tags = "医疗诊所机构接口")
@Controller
@RequestMapping(value = "/organization")
public class OrganizationController {

    private static final Logger logger = LoggerFactory.getLogger(OrganizationController.class);

    @Resource
    private OrganizationService organizationService;

    /*
     * @Author RAICOM2023
     * @Description //附近的医药/医疗机构
     * @Date 21:40 2023/3/6
     * @Param [nearbyOrganizationRequestVO, request]
     * @return com.hzxy.yiyid.web.common.base.BaseResult<?>
     **/
    @ApiOperation(value = "附件的医疗机构",notes = "附件的医疗机构")
    @RequestMapping(value = "/getNearbyOrganization", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult<?> getNearbyOrganization(@RequestBody NearbyOrganizationRequestVO nearbyOrganizationRequestVO
            , HttpServletRequest request) {
        try{
            if(null==nearbyOrganizationRequestVO.getType()){
                return BaseResult.fail(ErrorCodeEnum.ORG_PARAM_NULL_ERROR.getErrorCode(),ErrorCodeEnum.ORG_PARAM_NULL_ERROR.getMsg());
            }
            if (null == nearbyOrganizationRequestVO.getPageNo()) {
                nearbyOrganizationRequestVO.setPageNo(1);
            }
            if (null == nearbyOrganizationRequestVO.getPageSize()) {
                nearbyOrganizationRequestVO.setPageSize(20);
            }
            Map<String, Object> returnMap = new HashMap<>();
            List<Map<String, Object>> nearbyOrganizationMapList =
                    organizationService.listNearbyOrganization(nearbyOrganizationRequestVO,request);
            long total = organizationService.findNearbyOrganizationTotal(nearbyOrganizationRequestVO,request);
            returnMap.put("list", nearbyOrganizationMapList);
            returnMap.put("total", total);
            return BaseResult.success(returnMap);
        }catch(Exception e){
            logger.error(ErrorCodeEnum.SYSTEM_ERROR.getMsg(),e);
            return BaseResult.error(ErrorCodeEnum.SYSTEM_ERROR.getErrorCode(),ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }

    @ApiOperation(value = "附件定点的医疗机构",notes = "附件定点的医疗机构")
    @RequestMapping(value = "/getNearbyFeverClinic", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult<?> getNearbyFeverClinic(@RequestBody NearbyOrganizationRequestVO nearbyOrganizationRequestVO
            , HttpServletRequest request) {
        try{
            if (null == nearbyOrganizationRequestVO.getPageNo()) {
                nearbyOrganizationRequestVO.setPageNo(1);
            }
            if (null == nearbyOrganizationRequestVO.getPageSize()) {
                nearbyOrganizationRequestVO.setPageSize(20);
            }
            Map<String, Object> returnMap = new HashMap<>();
            List<Map<String, Object>> nearbyFeverClinicMapList =
                    organizationService.listNearbyFeverClinic(nearbyOrganizationRequestVO,request);
            long total = organizationService.findNearbyFeverClinicTotal(nearbyOrganizationRequestVO,request);
            returnMap.put("list", nearbyFeverClinicMapList);
            returnMap.put("total", total);
            return BaseResult.success(returnMap);
        }catch(Exception e){
            logger.error(ErrorCodeEnum.SYSTEM_ERROR.getMsg(),e);
            return BaseResult.error(ErrorCodeEnum.SYSTEM_ERROR.getErrorCode(),ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }

}
